﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TestRaptorDB.src;

namespace TestRaptorDB
{
    public partial class Form1 : Form
    {
        static RaptorDB.RaptorDB rdb; // 1 instance
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            rdb = RaptorDB.RaptorDB.Open("data"); // a "data" folder beside the executable
            RaptorDB.Global.RequirePrimaryView = false;
            //rdb.RegisterView(new SalesInvoiceView());
            DoWork();

            richTextBox1.AppendText("\r\nShutting down...");
            rdb.Shutdown(); // explicit shutdown
        }
        void DoWork()
        {
            richTextBox1.AppendText("\r\nInserting 100,000 documents...");
            int count = 100000;
            for (int i = 0; i < count; i++)
            {
                var inv = CreateInvoice(i);
                // save here
                rdb.Save(inv.ID, inv);
            }
            Console.WriteLine("done.");
        }
        SalesInvoice CreateInvoice(int counter)
        {
            // new invoice
            var inv = new SalesInvoice()
            {
                Date = DateTime.Now,
                Serial = counter % 10000,
                CustomerName = "Cust-" + counter,
                //NoCase = "Me " + counter % 10,
                Status = (byte)(counter % 4),
                Address = "street - " + counter

            };
            // new line items
            inv.Items = new List<LineItem>();
            for (int k = 0; k < 5; k++)
                inv.Items.Add(new LineItem()
                {
                    Product = "prod " + k,
                    Discount = 0,
                    Price = 10 + k,
                    QTY = 1 + k
                });
            return inv;
        }
    }
}
